<template>
  <div>
    <el-container>
      <el-header>
        <h1>用户管理</h1>
      </el-header>
      <el-main>
        <el-divider></el-divider>
        <el-table :data="tableData" style="width: 100%" border >
          <el-table-column   prop="id" label="用户id"  width="150">
          </el-table-column>
          <el-table-column prop="userName" label="用户名"  width="150">
          </el-table-column>
          <el-table-column prop="passWord" label="用户密码"  width="150">
          </el-table-column>
          <el-table-column prop="userRole" label="用户角色" width="150">
          </el-table-column>
          <el-table-column prop="userNumber" label="学号"  width="200">
          </el-table-column>
          <el-table-column prop="phoneNumber" label="手机号" width="200">
          </el-table-column>
          <el-table-column label="操作" width="250">
            <template slot-scope="scope">
              <el-popconfirm
                  title="确定要删除用户吗？"
                  @confirm="deleteUser(scope.row)"
              >
                <el-button slot="reference" type="danger" size="mini" >删除用户<i class="el-icon-edit"></i></el-button>
              </el-popconfirm>
              &nbsp;&nbsp;&nbsp;
              <el-button type="primary" size="mini" @click="updateMethod(scope.row)">更新资料<i class="el-icon-edit"></i></el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-dialog
            title="提示"
            :visible.sync="updateMessage"
            width="30%"
            :before-close="(el)=>handleClose(el,form1)">
          <el-form ref="reForm1" :model="form1" label-width="80px">
          <el-form-item label="用户id" prop="id">
              <el-input v-model="form1.id"></el-input>
            </el-form-item>
            <el-form-item label="用户名" prop="userName">
              <el-input v-model="form1.userName"></el-input>
            </el-form-item>
            <el-form-item label="用户密码" prop="passWord">
              <el-input v-model="form1.passWord"></el-input>
            </el-form-item>
            <el-form-item label="用户角色" prop="userRole">
              <el-input v-model="form1.userRole"></el-input>
            </el-form-item>
            <el-form-item label="学号" prop="userNumber">
              <el-input v-model="form1.userNumber"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phoneNumber">
              <el-input v-model="form1.phoneNumber"></el-input>
            </el-form-item>

          </el-form>
          <span slot="footer" class="dialog-footer">
          <el-button type="danger" @click="doUpdate('reject')">提交更新</el-button>
            <el-button type="primary" @click="updateMessage=false">取消</el-button>
           </span>
        </el-dialog>



      </el-main>
      <el-footer>
        <div style="padding: 10px 0">
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pageNum"
              :page-sizes="[10, 20, 30, 40,50,60,70,80,90,100]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
          </el-pagination>
        </div>
      </el-footer>
    </el-container>


  </div>
</template>
<script>
import request from '@/utils/request'
/*import handleClose from '@/utils/common'*/
import {getCTime} from "@/utils/common";
import axios from "axios";
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'verifyAdmin',
  data () {
    return {
      userName:'',
      pageNum:1,
      pageSize:10,
      checkDialog:false,
      total:0,
      updateMessage: false,
      form1:{
        id:'',
        userName:'',
        passWord:'',
        userRole:'',
        userNumber:'',
        phoneNumber: '',

      },
      sortProp: 'id',
      sortable: 'ascending',
      tableData:{}
    }
    },
  created () {
    this.userName=this.$route.params.userName
    console.log('跳转的userName是: '+this.userName)
    this.loadPage('admin')
  },
  methods: {
    handleSizeChange (pageSize) {
      this.pageSize = pageSize
      this.loadPage('admin')
    },
    handleCurrentChange (pageNum) {
      this.pageNum = pageNum
      this.loadPage('admin')
    },
    deleteUser(row){
      if (row.userName==='admin') {
        this.$message.error('admin 用户不能删除！！')
        return
      }


      request.get('http://localhost:4009/admin/user/delete', {
        params: {
          id:row.id
        }
      }).then(res => {
        console.log(res)
        if (res.status !== 200){
          this.$message.error(res.message)
        }else {
          this.$message.success(res.message)
          this.loadPage("admin");
        }
      })


    },
    loadPage (userType) {
      if (userType === 'admin'){
        console.log("sortProp: "+this.sortProp)
        request.get('http://localhost:4009/admin/user/showAll', {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            sortProp: this.sortProp,
            sortable: this.sortable
          }
        }).then(res => {
          console.log(res)
          if (res.status !== 200){
            this.$message.error(res.message)
          }else {
            this.tableData = res.data
            this.total = res.total
          }

        })
      }
    },
    doUpdate(){
      this.updateMessage=false
      let submitData = {
        id:this.form1.id,
        userName:this.form1.userName,
        passWord:this.form1.passWord,
        userRole:this.form1.userRole,
        userNumber:this.form1.userNumber,
        phoneNumber:this.form1.phoneNumber,
        updateTime:getCTime()
      }
      axios.post('http://localhost:4009/admin/user/update', submitData).then(res => {
        console.log(res)
        if (res.data.status !== 200) {
          this.$message.error(res.data.message)
        } else {
          this.$message({
            message: '更新成功！！',
            type: 'success'
          })
        }
        this.$refs.reForm1.resetFields()
        this.loadPage("admin")
      }).catch(error => {
        console.log(error)
        this.$message.error(error)
      })

    },
    updateMethod(row){
      this.form1.id=row.id
      this.form1.userName=row.userName
      this.form1.passWord=row.passWord
      this.form1.userRole=row.userRole
      this.form1.userNumber=row.userNumber
      this.form1.phoneNumber=row.phoneNumber
      this.updateMessage=true;
    },
    handleClose(done){
      // eslint-disable-next-line no-unused-vars
      this.$confirm('确认关闭？').then(_ => {
              this.checkDialog=false
            this.$refs.reForm1.resetFields()
            done()
        // eslint-disable-next-line no-unused-vars
          }).catch(_ => {})
    }
  }
}
</script>
